package com.zl.smartmall.library.listener;

import java.util.List;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.zl.smartmall.library.concurrent.ThreadExecutor;
import com.zl.smartmall.library.po.AdvertInfo;
import com.zl.smartmall.library.po.BrandedProductsInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.vo.AdvertInfoDao;
import com.zl.smartmall.library.vo.BrandedProductsInfoDao;

/**
 * 品牌商品列表数据请求监听类
 * @author 陈希然
 *
 */
public abstract class BrandedProductsListener extends BaseListener {

	private static final String TAG = BrandedProductsListener.class.getSimpleName();
	
	public int activitiesId;
	private Context context;
	
	public BrandedProductsListener(Context context, RequestCode requestCode) {
		super(requestCode);
		this.context = context;
	}
	
	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		
			try {
				int status = response.getInt(Constants.REQ_STATUS);
				String msg = response.getString(Constants.REQ_MSG);
				CrashHandler.d(TAG, "品牌商品列表服务器status = " + status);
				CrashHandler.d(TAG, "品牌商品列表服务器msg = " + msg);
				
				switch(status){
				case Constants.REQ_OK:
					JSONObject jsonObject = response.getJSONObject(Constants.REQ_RESULT);
					List<AdvertInfo> advertInfos = AdvertInfo.parseArray(jsonObject.getJSONArray("banners"), Constants.ADVERT_AFFILIATION_BRANDED_PRODUCTS);
					List<BrandedProductsInfo> brandedProductsInfos = BrandedProductsInfo.parseArray(jsonObject.getJSONArray("products_list"), activitiesId);
					onResponseSuccess(advertInfos, brandedProductsInfos);
					if(requestCode == RequestCode.PULL) {
						saveCache2DB(advertInfos, brandedProductsInfos);
					}
					break;
				
				case Constants.REQ_OFFLINE:
					onOffline();
					break;
					
				case Constants.REQ_NO_MORE_DATA:
					onNoMoreData();
					break;
					
				default:
					onResponseFailed();
				}
				
			} catch (JSONException e) {
				onFailure(statusCode, headers, e, new JSONObject());
			}
	}
	
	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		// TODO Auto-generated method stub
		CrashHandler.e(throwable);
		onResponseFailed();
	}
	
	private void saveCache2DB(final List<AdvertInfo> advertInfos, final List<BrandedProductsInfo> brandedProductsInfos) {
		ThreadExecutor.getInstance().execute(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				CrashHandler.d(TAG, "缓存品牌商品列表数据");
				if(brandedProductsInfos.size() > 0) {
					AdvertInfoDao advertInfoDao = new AdvertInfoDao(context);
					advertInfoDao.delete(Constants.ADVERT_AFFILIATION_BRANDED_PRODUCTS);
					advertInfoDao.insert(advertInfos);
					BrandedProductsInfoDao brandedProductsInfoDao = new BrandedProductsInfoDao(context);
					brandedProductsInfoDao.delete(brandedProductsInfos.get(0).getActivitiesId());
					brandedProductsInfoDao.insert(brandedProductsInfos);
				}
			}
		});
	}
	
	public abstract void onResponseSuccess(List<AdvertInfo> advertInfos, List<BrandedProductsInfo> brandedProductsInfos);
	
}
